package com.smartsofa.main;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.smartsofa.R;
public class UserMessage extends Activity{
	
	private TextView tv_message;
	private ListView lv_message;
	private Button bt_message_back;
	private Button bt_clear;      
	private Boolean message_flag; // index of whether has message
	private JSONArray message;
	private JSONArray replyMessageArray;
	private LayoutInflater layoutInflater;
	public static  List<Map<String, Object>> msglist;
	private int indexOfHis = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {	
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message);
		tv_message = (TextView) findViewById(R.id.tv_message);
		lv_message = (ListView) findViewById(R.id.lv_message);
		bt_message_back = (Button) findViewById(R.id.bt_message_back);
		bt_clear = (Button) findViewById(R.id.bt_clearmeassage);
		layoutInflater = this.getLayoutInflater();
		
		message = MainActivity.message;
		replyMessageArray = MainActivity.replyMessageArray;
		msglist = new ArrayList<Map<String, Object>>();		
		// when back button is clicked , return
		bt_message_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		// when clear button is clicked ,clear all the message
		bt_clear.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			}
		});
		System.out.println("message:---"+message);
		if(message != null || replyMessageArray !=null){
			getdata();
			lv_message.setAdapter(new SimpleAdapter(this,msglist,R.layout.listrow_message,new String[]{"familyname","username","msgstate"},
					new int[]{R.id.m_familyname,R.id.m_yaoqingname,R.id.msg_state}){
				@Override
				public View getView(int position, View convertView,
						ViewGroup parent) {
					if(convertView == null){
						convertView = layoutInflater.inflate(R.layout.listrow_message, null);
					}
					ImageView msgImageView = (ImageView) convertView.findViewById(R.id.im_message);
					if(position>=indexOfHis){
						msgImageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.message_off));
					}
					return super.getView(position, convertView, parent);
				}
			});
			tv_message.setVisibility(View.INVISIBLE);
		}
		else{
			tv_message.setVisibility(View.VISIBLE);	
		}
		lv_message.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				view.setTag(R.id.m_familyname, 1);
				Intent intent = new Intent();
				intent.setClass(UserMessage.this, MessagePage.class);
//				intent.putExtras(bundle);
				intent.putExtra("position", position);
				startActivity(intent);
			}
		});
		
	}
	private void getdata() {
		List<Map<String, Object>> oldDataList = new ArrayList<Map<String, Object>>();
		if(message!=null&&message.length()>0){
			for(int i = 0;i<message.length();i++){
				Map<String,Object> map = new HashMap<String, Object>();
				try {
					map.put("familyname", message.getJSONObject(i).getString("familyname"));
					map.put("username", message.getJSONObject(i).getString("username"));
					map.put("msgstate", "邀请你");
					map.put("sign", message.getJSONObject(i).getString("sign"));
					map.put("verifyMsg", message.getJSONObject(i).getString("verifyMsg"));
					map.put("familyid", message.getJSONObject(i).getString("familyid"));
					
					map.put("validateStatus", message.getJSONObject(i).getString("validateStatus"));
					if(message.getJSONObject(i).getInt("validateStatus") == 0){
						msglist.add(map);
						indexOfHis++;
					}
					else{
						oldDataList.add(map);
					}
					
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
		
		int index = 0;
		if(replyMessageArray!=null&&replyMessageArray.length()>0){
			for(int i = 0;i<replyMessageArray.length();i++){
				Map<String,Object> map = new HashMap<String, Object>();
				try {
					
					map.put("familyname", replyMessageArray.getJSONObject(i).getString("familyname"));
					map.put("username", replyMessageArray.getJSONObject(i).getString("invitename"));
					map.put("validateStatus",replyMessageArray.getJSONObject(i).getInt("message"));
					index = replyMessageArray.getJSONObject(i).getInt("flag");
					if(index == 1){
						map.put("msgstate", "同意");	
					}else{
						map.put("msgstate", "拒绝");
					}
					if(replyMessageArray.getJSONObject(i).getInt("message")==0){
						msglist.add(map);	
						indexOfHis++;
					}
					else{
						oldDataList.add(map);
					}
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
		
		for(int i = 0;i<oldDataList.size();i++){
			msglist.add(oldDataList.get(i));
		}
		
	}

}
